<script>
  var floodFill = function(image, sr, sc, color) {
    if (image[sr][sc] === color){
        return image;
    }
    const oldColor = image[sr][sc]
    function dfs(x, y) {
        if (x < 0 || x >= image.length || y < 0 || y >= image[0].length || image[x][y] !== oldColor){
            return;
        }
        image[x][y] = color;

        dfs(x + 1, y);
        dfs(x - 1, y);
        dfs(x, y + 1);
        dfs(x, y - 1);
    }
    dfs(sr, sc)
    return image;
  };

  console.log(floodFill([[1,1,1],[1,1,0],[1,0,1]], 1, 1, 2));
  console.log(floodFill([[0,0,0],[0,0,0]], 0, 0, 2))
  console.log(floodFill([[0,0,0],[0,0,0]], 0, 0, 0))
</script>